package com.itbaima.controller;

import com.itbaima.entity.ResponseResult;
import com.itbaima.entity.User;
import com.itbaima.resolver.CurrentUserId;
import com.itbaima.service.UserService;
import com.itbaima.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
//@CrossOrigin // 解决跨域请求
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/insertUser")
    private ResponseResult insertUser(){
        userService.insertUser();
        return new ResponseResult(200, "添加成功");
    }

    @RequestMapping("/findAll")
    public ResponseResult<List<User>> findAll(@CurrentUserId String userId /*HttpServletRequest request*/) throws Exception {
        // 获取请求头中的token
//        String token = request.getHeader("token");
//        if (StringUtils.hasText(token)) {
//            // 解析token获取用户ID
//            Claims claims = JwtUtil.parseJWT(token);
//            String userId = claims.getSubject();
//            System.out.println(userId);
//        }
        System.out.println("登录用户的ID：" + userId);
        // 调用service查询数据进行返回
        List<User> users = userService.findAll();
        return new ResponseResult<>(200, users);
    }
}
